unit supplierstock;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, Grids, StdCtrls, Buttons, hpdf, hpdf_types, fpstructs;

type

  { TSupplierStockForm }

  TSupplierStockForm = class(TForm)
    BitBtn1: TBitBtn;
    EmailEdit: TEdit;
    TelephoneEdit: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    OrderNumEdit: TEdit;
    ContactEdit: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    InvoiceMemo: TMemo;
    DeliveryMemo: TMemo;
    OrderButton: TButton;
    InfoPanel: TPanel;
    ButtonPanel: TPanel;
    Label1: TLabel;
    SaveDialog: TSaveDialog;
    SupplierNameLabel: TLabel;
    StockGrid: TStringGrid;
    procedure CalculateOrderCost ( Sender: TObject ) ;
    procedure OrderButtonClick ( Sender: TObject ) ;
    procedure StockGridPrepareCanvas ( sender: TObject; aCol, aRow: Integer;
      aState: TGridDrawState ) ;
  private
    { private declarations }
  public
    { public declarations }
    OurCompanyName: string;
    OurCompanyAddress: string;
    SupplierIndex: Integer;
    procedure CreateOrder(fn: TFilename);
  end;

var
  SupplierStockForm: TSupplierStockForm;

{NOTE: This code is the same as that in Server/fpreports.pas - should be placed in common source}
procedure DrawText( canvas: HPDF_Page; x, y, w: Double; fontName: HPDF_Font; fontsize: Integer; txt: PChar; justify: Integer; underline:Boolean);
procedure HandleHPDFError(error_no: HPDF_STATUS; detail_no: HPDF_STATUS;
                user_data: Pointer); cdecl;

const
   JUSTIFY_LEFT   = 0;
   JUSTIFY_CENTER = 1;
   JUSTIFY_RIGHT  = 2;

implementation

uses main, math;

procedure DrawText ( canvas: HPDF_Page; x, y, w: Double; fontName: HPDF_Font;
  fontsize: Integer; txt: PChar; justify: Integer; underline: Boolean ) ;
var
  ln: Integer;
  rw: Single;
  lx: Double;
begin
  lx := x;
  // Set font
  HPDF_Page_SetFontAndSize(canvas, fontName, fontSize);

  HPDF_Page_BeginText(canvas);

  // Truncate the text to max col width
  ln := HPDF_Page_MeasureText(canvas, txt, w, 0, @rw);
//  MID$(txt, ln+1) = CHR$(0)

  // Justify
  IF justify = JUSTIFY_RIGHT THEN
      // Right justify the text
      lx := x + (w - rw)
  ELSE IF justify = JUSTIFY_CENTER THEN
      // Center justify the text
      lx := x + (w - rw) / 2;

  // Draw the text
  HPDF_Page_MoveTextPos(canvas, lx, y);
  HPDF_Page_ShowText(canvas, txt);
  HPDF_Page_EndText(canvas);

  // Underline the text if flag is set
  IF underline THEN
      begin
      HPDF_Page_MoveTo(canvas, x, y-1);
      HPDF_Page_LineTo(canvas, x + w, y-1);
      HPDF_Page_Stroke(canvas);
      end;
end;

procedure HandleHPDFError ( error_no: HPDF_STATUS; detail_no: HPDF_STATUS;
  user_data: Pointer ) ; cdecl;
begin
  {$IFDEF DEBUG} WriteLn('HPDF Error ', error_no, ' - detail ', detail_no); {$ENDIF}
end;

{ TSupplierStockForm }

procedure TSupplierStockForm.CalculateOrderCost ( Sender: TObject ) ;
var
  tot: Double;
  cost: Double;
  order: Integer;
  r: Integer;
begin
  tot := 0;
  For r := 1 to StockGrid.RowCount-1 do
    begin
      StockGrid.Cells[4, r] := Trim(StockGrid.Cells[4, r]);
      StockGrid.Cells[6, r] := Trim(StockGrid.Cells[6, r]);
      if TryStrToInt( StockGrid.Cells[6, r], order )
         then if TryStrToFloat(StockGrid.Cells[4, r], cost)
      then tot := tot + (cost * order);
    end;
  {$IFDEF MSWINDOWS} StockGrid.Refresh; {$ENDIF}
   Label1.Caption := 'Order value: £ ' + FormatFloat('0.00', tot);
end;

procedure TSupplierStockForm.OrderButtonClick ( Sender: TObject ) ;
begin
  SaveDialog.FileName := 'Order ' + OrderNumEdit.Text + '-' + Copy(SupplierNameLabel.Caption, Length('Supplier:  '), MaxInt) + '.pdf';
  if SaveDialog.Execute then
      begin
        CreateOrder(SaveDialog.FileName);
        ModalResult := mrOK;
      end;
end;

procedure TSupplierStockForm.StockGridPrepareCanvas ( sender: TObject; aCol,
  aRow: Integer; aState: TGridDrawState ) ;
begin
  if StockGrid.Cells[6, aRow]<>'' then
    begin
      StockGrid.Canvas.Font.Style := [fsBold];
      StockGrid.Canvas.Brush.Color := clSkyBlue;
    end;
end;

procedure TSupplierStockForm.CreateOrder ( fn: TFilename ) ;
const
   DEFAULT_FONTSIZE = 10;
   PAGE_MARGIN = 60;
var
  pdf: HPDF_Doc;
  page: HPDF_Page;
  createdate: THPDF_Date;
  pages: TList;
  samp_text: String;
  afont: HPDF_Font;
  aheight: HPDF_REAL;
  awidth: HPDF_REAL;
  def_font: HPDF_Font;
  tw: HPDF_REAL;
  page_title: String;
  i: Integer;
  SQL: String;
  tx: String;
  tab0: Integer;
  tab1: Integer;
  tab2: Integer;
  x: Integer;
  line_y: Single;
  bold_font: HPDF_Font;
  sometext: String;
  supplier: TfpSupplierInfo;
  line_y1: Single;
  contactname: String;
  r: Integer;
  createtime: TDateTime;

  procedure NewPage;
  begin
    page := HPDF_AddPage (pdf);
    pages.Add(page);
    line_y := aheight-100;
    DrawText(page, tab0, line_y, awidth-PAGE_MARGIN-tab0, bold_font, DEFAULT_FONTSIZE+1, PChar('Item Ref.'), JUSTIFY_LEFT, false);
    DrawText(page, tab1, line_y, 100, bold_font, DEFAULT_FONTSIZE+1, PChar('Item'), JUSTIFY_LEFT, false);
    DrawText(page, tab2, line_y, 90, bold_font, DEFAULT_FONTSIZE+1, PChar('Quantity'), JUSTIFY_CENTER, false);
    line_y := line_y-20;
  end;

begin
  pages := TList.Create;
  pdf := HPDF_New(@HandleHPDFError, nil);
  HPDF_SetCompressionMode (pdf, $0F);
  def_font := HPDF_GetFont(pdf, PChar('Helvetica'), nil);
  bold_font := HPDF_GetFont(pdf, PChar('Helvetica-Bold'), nil);
  x := HPDF_SetInfoAttr(pdf, HPDF_INFO_CREATOR, PChar(FIREPOS_INFO));
  x := HPDF_SetInfoAttr(pdf, HPDF_INFO_AUTHOR, PChar(ContactEdit.Text));
  createtime := Now;
  with createdate do
  begin
    year := StrToInt(FormatDateTime('yyyy', createtime));
    month := StrToInt(FormatDateTime('m', createtime));
    day := StrToInt(FormatDateTime('d', createtime));
    hour:= StrToInt(FormatDateTime('h', createtime));
    minutes := StrToInt(FormatDateTime('n', createtime));
    seconds := StrToInt(FormatDateTime('s', createtime));
    ind := '+';
    off_hour := 0;
    off_minutes := 0;
  end;
  x := HPDF_SetInfoDateAttr(pdf, HPDF_INFO_CREATION_DATE, createdate);
  x := HPDF_SetInfoAttr(pdf, HPDF_INFO_TITLE, PChar('Order ' + OrderNumEdit.Text + '-' + Copy(SupplierNameLabel.Caption, Length('Supplier:  '), MaxInt)));

  supplier := MainForm.SupplierList[SupplierIndex];


  page := HPDF_AddPage (pdf);
  HPDF_Page_SetFontAndSize (page, bold_font, 18);
  page_title := 'ORDER FORM';
  aheight := HPDF_Page_GetHeight (page);
  awidth := HPDF_Page_GetWidth (page);
  line_y := aheight-PAGE_MARGIN;
  HPDF_Page_SetRGBFill(page, 0.2, 0, 1.0);
  tw := HPDF_Page_TextWidth (page, PChar(page_title));
  HPDF_Page_BeginText (page);
  HPDF_Page_TextOut (page, PAGE_MARGIN, line_y, PChar(page_title));
  HPDF_Page_EndText (page);
  HPDF_Page_SetRGBFill(page, 0, 0, 0.0);

  line_y := line_y-25;
  tab0 := PAGE_MARGIN;
  tab1 := Round(awidth) div 2;

  HPDF_Page_SetGrayStroke(page, 0.2);
  HPDF_Page_SetLineCap(page, HPDF_ROUND_END);
  HPDF_Page_SetLineWidth(page, 4);
  HPDF_Page_MoveTo(page, tab0, line_y);
  HPDF_Page_LineTo(page, awidth-PAGE_MARGIN, line_y);
  HPDF_Page_Stroke(page);
  HPDF_Page_SetLineWidth(page, 0.75);
  HPDF_Page_SetRGBStroke(page, 0, 0, 0.0);
  line_y := line_y-40;

  { Vendor Details}
  DrawText(page, tab0, line_y, awidth, bold_font, 14, PChar('TO:'), JUSTIFY_LEFT, false);
  line_y := line_y - 16;
  line_y1 := line_y;
  DrawText(page, tab0, line_y, awidth, def_font, DEFAULT_FONTSIZE, PChar(supplier.name), JUSTIFY_LEFT, false);
  line_y := line_y - DEFAULT_FONTSIZE-4;
  sometext := supplier.address;
  x := Pos(LineEnding, sometext);
  while x>0 do
        begin
          DrawText(page, tab0, line_y, 100, def_font, DEFAULT_FONTSIZE, PChar(Copy(sometext, 1, x-1)), JUSTIFY_LEFT, false);
          Delete(sometext, 1, x+Length(LineEnding)-1);
          x := Pos(LineEnding, sometext);
          line_y := line_y - DEFAULT_FONTSIZE-4;
        end;
  DrawText(page, tab0, line_y, 100, def_font, DEFAULT_FONTSIZE, PChar(sometext), JUSTIFY_LEFT, false);
  line_y := line_y -  DEFAULT_FONTSIZE-4;
  DrawText(page, tab0, line_y, 100, def_font, DEFAULT_FONTSIZE, PChar(supplier.postcode), JUSTIFY_LEFT, false);
  line_y := line_y - 40;

  { Date and Reference numbers }
  tab2 := tab1 + 80;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Date:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar(FormatDateTime('d mmm yyyy', Date) ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-20;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Account no.:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar( supplier.accountnum ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-20;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Order no.:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar( OrderNumEdit.Text ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-20;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Contact:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar( ContactEdit.Text ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-DEFAULT_FONTSIZE-4;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Tel.:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar( TelephoneEdit.Text ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-DEFAULT_FONTSIZE-4;
  DrawText(page, tab1, line_y1, 100, bold_font, DEFAULT_FONTSIZE, PChar('Email:' ), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar( EmailEdit.Text ), JUSTIFY_LEFT, false);
  line_y1 := line_y1-20;

  line_y := Min(line_y, line_y1)-20;

  {Buyer Details}
  DrawText(page, tab0, line_y, 100, bold_font, 14, PChar('Invoice to:'), JUSTIFY_LEFT, false);
  DrawText(page, tab1, line_y, 100, bold_font, 14, PChar('Deliver to:'), JUSTIFY_LEFT, false);
  line_y := line_y - 16;
  line_y1 := line_y;
  HPDF_Page_SetRGBFill(page, 0, 0, 0.0);
  HPDF_Page_SetRGBStroke(page, 0, 0, 0.0);
  for x := 0 to InvoiceMemo.Lines.Count-1 do
    begin
      DrawText(page, tab0, line_y, 100, def_font, DEFAULT_FONTSIZE, PChar(InvoiceMemo.Lines[x]), JUSTIFY_LEFT, false);
      line_y := line_y -  DEFAULT_FONTSIZE-4;
    end;
  for x := 0 to DeliveryMemo.Lines.Count-1 do
    begin
      DrawText(page, tab1, line_y1, 100, def_font, DEFAULT_FONTSIZE, PChar(DeliveryMemo.Lines[x]), JUSTIFY_LEFT, false);
      line_y1 := line_y1 -  DEFAULT_FONTSIZE-4;
    end;

  line_y := Min(line_y, line_y1)-20;

  {Order Details}
  tab1 := PAGE_MARGIN + 100;
  tab2 := Round(awidth-PAGE_MARGIN-100);
  DrawText(page, tab0+5, line_y, 100, bold_font, DEFAULT_FONTSIZE+1, PChar('Item Ref.'), JUSTIFY_LEFT, false);
  DrawText(page, tab1, line_y, 100, bold_font, DEFAULT_FONTSIZE+1, PChar('Item'), JUSTIFY_LEFT, false);
  DrawText(page, tab2, line_y, 90, bold_font, DEFAULT_FONTSIZE+1, PChar('Quantity'), JUSTIFY_CENTER, false);
  line_y := line_y-20;
  for r := 1 to StockGrid.RowCount-1 do
    if Trim(StockGrid.Cells[6, r])<>'' then    // quantity specified
        if Trim(StockGrid.Cells[1, r])<>'' then   // item specified
          begin
            DrawText(page, tab0+5, line_y, 90, def_font, DEFAULT_FONTSIZE, PChar(StockGrid.Cells[0, r]), JUSTIFY_LEFT, false);
            DrawText(page, tab1, line_y, awidth-PAGE_MARGIN-tab1-100, def_font, DEFAULT_FONTSIZE, PChar(StockGrid.Cells[1, r]), JUSTIFY_LEFT, false);
            DrawText(page, tab2, line_y, 90, def_font, DEFAULT_FONTSIZE, PChar(StockGrid.Cells[6, r]), JUSTIFY_CENTER, false);
            HPDF_Page_Rectangle(page, tab0, line_y-5, Round(awidth-(PAGE_MARGIN*2)), 20);
            HPDF_Page_Stroke(page);
            line_y := line_y-20;
            if line_y<PAGE_MARGIN then NewPage;
          end;

    {number pages}
    for x := 0 to pages.Count-1 do
      begin
        page := HPDF_Page(Pages[x]);
        sometext := OurCompanyName + ' Order no.' + OrderNumEdit.Text
               + '   Page ' + IntToStr(x+2) + ' of ' + IntToStr(Pages.Count+1);
        DrawText(page, tab0, aheight-PAGE_MARGIN, awidth-(PAGE_MARGIN*2), def_font, 9, PChar(sometext), JUSTIFY_RIGHT, false);
      end;

  HPDF_SaveToFile(pdf, PChar(fn));
  pages.Free;
  HPDF_FreeDocAll(pdf);
end;

initialization
  {$I supplierstock.lrs}

end.

